﻿using blackhouse.Locations;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace blackhouse.StareKino.Web.Contact {
    internal class ContactPlace {

        #region Fields

        private const string TABLE_NAME = "CONTACT_PLACES";

        #endregion

        #region Properties

        public int PlaceId { get; private set; }
        public string Name { get; private set; }
        public string Subtitle { get; private set; }
        public string Street { get; private set; }
        public Location PlaceLocation { get; private set; }
        public ContactPlaceCategory Category { get; private set; }

        #endregion

        #region Constructors

        public ContactPlace(GroupRecord contactPlaceRecord) {
            this.PlaceId = contactPlaceRecord.RecordId;
            this.Name = contactPlaceRecord.GetString("place_name");
            if (contactPlaceRecord.IsNotNull("place_subtitle"))
                this.Subtitle = contactPlaceRecord.GetString("place_subtitle");
            else
                this.Subtitle = String.Empty;
            this.Street = contactPlaceRecord.GetString("place_street");
            this.PlaceLocation = contactPlaceRecord.GetCustomValue<Location>("place_location");
            this.Category = new ContactPlaceCategory(contactPlaceRecord.GetRecord("category"));
        }

        #endregion

        #region Methods

        public static ContactPlaces GetPlaces() {
            GroupRecords records = new GroupRecords(TABLE_NAME);
            return new ContactPlaces(
                records.Select(gr => new ContactPlace(gr))
                );
        }

        internal Distance GetDistance(Location location) {
            return HaversineAlgorithm.GetDistance(location, this.PlaceLocation);
        }

        #endregion

    }
}